package com.yupi.springbootinit.manager;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.datasource.*;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.search.SearchRequest;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.model.vo.SearchVO;
import com.yupi.springbootinit.model.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 搜索门面
 * @author gan
 * @date 2023/4/21 10:55
 */
@Component
@Slf4j
public class SearchFacade {
    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private UserDataSource userDataSource;

    @Resource
    private PostDataSource postDataSource;

    @Resource
    private DataSourceRegistry dataSourceRegistry;

    public SearchVO searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request){
        String searchText = searchRequest.getSearchText();
        long pageNum = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();

        String type = searchRequest.getType();
        SearchTypeEnum typeEnum = SearchTypeEnum.getEnumByValue(type);
        ThrowUtils.throwIf(StringUtils.isBlank(type), ErrorCode.PARAMS_ERROR);
        //当前端传入的type为空的时候去查找全部
        if (typeEnum == null){
            //使用多线程去并发查找
            CompletableFuture<Page<UserVO>> userTask =
                    CompletableFuture.supplyAsync(() -> userDataSource.doSearch(searchText,pageNum,pageSize));

            CompletableFuture<Page<Picture>> picTask =
                    CompletableFuture.supplyAsync(() -> pictureDataSource.doSearch(searchText,pageNum,pageSize));

            CompletableFuture<Page<PostVO>> postTask =
                    CompletableFuture.supplyAsync(() -> postDataSource.doSearch(searchText,pageNum,pageSize));

            //当上面的所有任务完成后才回去执行后面的代码
            CompletableFuture.allOf(userTask,postTask,picTask).join();
            try {
                Page<UserVO> userVOPage = userTask.get();
                Page<Picture> picturePage = picTask.get();
                Page<PostVO> postVOPage = postTask.get();

                SearchVO searchVO = new SearchVO();
                searchVO.setPictureList(picturePage.getRecords());
                searchVO.setPostList(postVOPage.getRecords());
                searchVO.setUserList(userVOPage.getRecords());
                return searchVO;
            }catch (Exception e){
                log.error("查询异常",e);
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"查询异常");
            }
        }else {
            SearchVO searchVO = new SearchVO();
            //现根据类型去拿到一个数据源
            DataSource dataSource = dataSourceRegistry.getDataSourceByType(typeEnum.getValue());
            Page page = dataSource.doSearch(searchText,pageNum,pageSize);
            searchVO.setDataList(page.getRecords());
            return searchVO;
        }

    }
}
